<template>
  <div class="com-container">
    <div ref="seller" class="com-chart" />
  </div>
</template>
<script>
// import { getSeller } from '../api/sys'
import { mapState } from 'vuex'
export default {
  data() {
    return {
      sellerChart: null,
      allData: null,
      currentPage: 1,
      totalPage: 0,
      timerId: null
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      this.sellerChart.dispose() // 销毁当前的图表
      this.initChart() // 更换主题后重新渲染
      this.screenAdaptation() // 屏幕适配
      this.upDataChart() // 更新图表数据
    }
  },
  created() {
    this.$socket.registerCallBack('sellerData', this.getData)
  },
  mounted() {
    this.initChart()
    this.$socket.send({
      action: 'getData',
      socketType: 'sellerData',
      chartName: 'seller',
      value: ''
    })
    // this.getData()
    window.addEventListener('resize', this.screenAdaptation)
    this.screenAdaptation()
  },
  destroyed() {
    clearInterval(this.timerId)
    window.removeEventListener('resize', this.screenAdaptation)
    this.$socket.unRegisterCallBack('sellerData')
  },
  methods: {
    initChart() {
      this.sellerChart = this.$eCharts.init(this.$refs.seller, this.theme)
      const initOption = {
        title: {
          text: '▎商家销售统计',
          left: 20,
          top: 20
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            z: 0,
            lineStyle: {
              color: '#2D3443'
            }
          }
        },
        grid: {
          top: '20%',
          left: '3%',
          right: '6%',
          bottom: '3%',
          containLabel: true // 距离是包含坐标轴上的文字
        },
        xAxis: {
          type: 'value'
        },
        yAxis: {
          type: 'category'
        },
        series: [
          {
            type: 'bar',
            label: {
              show: true,
              position: 'right',
              textStyle: {
                color: 'white'
              }
            },
            itemStyle: {
              barBorderRadius: [0, 20, 20, 0],
              // 指明颜色渐变的方向
              // 指明不同百分比之下颜色的值
              color: new this.$eCharts.graphic.LinearGradient(0, 0, 1, 0, [
              // 百分之0状态之下的颜色值
                {
                  offset: 0,
                  color: '#5052EE'
                },
                // 百分之100状态之下的颜色值
                {
                  offset: 1,
                  color: '#AB6EE5'
                }
              ])
            }
          }
        ]
      }
      this.sellerChart.setOption(initOption)
      this.sellerChart.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      this.sellerChart.on('mouseout', () => {
        this.startSetInterval()
      })
    },
    // res : 服务端返回的数据 通过监听消息 后 最终处理的数据
    getData(res) {
      // this.allData = await getSeller()
      this.allData = res
      this.allData.sort((a, b) => {
        return a.value - b.value
      })
      this.totalPage = Math.ceil(this.allData.length / 5)
      this.upDataChart()
      this.startSetInterval()
    },
    upDataChart() {
      const start = (this.currentPage - 1) * 5
      const end = this.currentPage * 5
      const showData = this.allData.slice(start, end)
      const sellerNames = showData.map(item => { return item.name })
      const sellerValues = showData.map(item => { return item.value })
      const dataOption = {
        yAxis: {
          data: sellerNames
        },
        series: [{
          data: sellerValues
        }]
      }
      this.sellerChart.setOption(dataOption)
      this.startSetInterval()
    },
    startSetInterval() {
      if (this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        this.currentPage++
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1
        }
        this.upDataChart()
      }, 3000)
    },
    screenAdaptation() {
      const titleFontSize = this.$refs.seller.offsetWidth / 100 * 3.6
      const screenOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize * 1.2
          }
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize
            }
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0]
            }
          }
        ]
      }
      this.sellerChart.setOption(screenOption)
      this.sellerChart.resize()
    }
  }
}
</script>
